package MathLib.triangulation;

public class CheckClass {
	public static boolean CheckEllipseEq(double x0,double y0,double x1,double y1,double x2,double y2,double x3,double y3 ){
		double a,b,c,d;
		a = x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);	//(x2*y3-y2*x3)-(x1*y3-y1*x3)+(x1*y2-x2*y1);
		b = (x1*x1+y1*y1)*(y2-y3)+(x2*x2+y2*y2)*(y3-y1)+(x3*x3+y3*y3)*(y1-y2);	//(y3*(x2*x2+y2*y2) - y2*(x3*x3+y3*y3))-(y3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		c = (x1*x1+y1*y1)*(x2-x3)+(x2*x2+y2*y2)*(x3-x1)+(x3*x3+y3*y3)*(x1-x2);//(x3*(x2*x2+y2*y2) - x2*(x3*x3+y3*y3))-(x3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		d = (x1*x1+y1*y1)*(x2*y3-x3*y2)+(x2*x2+y2*y2)*(x3*y1-x1*y3)+(x3*x3+y3*y3)*(x1*y2-x2*y1);
		
		return (a*(x0*x0+y0*y0)-b*x0+c*y0-d)*a>=0;
	}
	
	public static boolean CheckEllipseEq(double x0,double y0,double a,double b,double c,double d){
		return (a*(x0*x0+y0*y0)-b*x0+c*y0-d)*a>=0;
	}
	
	public static void getCoefs(double x1,double y1,double x2,double y2,double x3,double y3,double a, double b, double c, double d ){
		a = x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);	//(x2*y3-y2*x3)-(x1*y3-y1*x3)+(x1*y2-x2*y1);
		b = (x1*x1+y1*y1)*(y2-y3)+(x2*x2+y2*y2)*(y3-y1)+(x3*x3+y3*y3)*(y1-y2);	//(y3*(x2*x2+y2*y2) - y2*(x3*x3+y3*y3))-(y3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		c = (x1*x1+y1*y1)*(x2-x3)+(x2*x2+y2*y2)*(x3-x1)+(x3*x3+y3*y3)*(x1-x2);//(x3*(x2*x2+y2*y2) - x2*(x3*x3+y3*y3))-(x3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		d = (x1*x1+y1*y1)*(x2*y3-x3*y2)+(x2*x2+y2*y2)*(x3*y1-x1*y3)+(x3*x3+y3*y3)*(x1*y2-x2*y1);
	}
	
	public static void getCoefsForCircle(double x1,double y1,double x2,double y2,double x3,double y3,double r, double Xc, double Yc) {
		double a,b,c,d;
		a = x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);	//(x2*y3-y2*x3)-(x1*y3-y1*x3)+(x1*y2-x2*y1);
		b = (x1*x1+y1*y1)*(y2-y3)+(x2*x2+y2*y2)*(y3-y1)+(x3*x3+y3*y3)*(y1-y2);	//(y3*(x2*x2+y2*y2) - y2*(x3*x3+y3*y3))-(y3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		c = (x1*x1+y1*y1)*(x2-x3)+(x2*x2+y2*y2)*(x3-x1)+(x3*x3+y3*y3)*(x1-x2);//(x3*(x2*x2+y2*y2) - x2*(x3*x3+y3*y3))-(x3*(x1*x1+y1*y1) - y1*(x3*x3+y3*y3))+(y2*(x1*x1+y1*y1) - y1*(x2*x2+y2*y2));
		d = (x1*x1+y1*y1)*(x2*y3-x3*y2)+(x2*x2+y2*y2)*(x3*y1-x1*y3)+(x3*x3+y3*y3)*(x1*y2-x2*y1);
		
		Xc = b/2.0/a;
		Yc = -c/2.0/2;
		r = Math.abs(Math.sqrt(b*b+c*c-4*a*d)/2/a);
		
	}
	
	public static boolean CheckPredefinedCircle(double x0,double y0,double Xc,double Yc,double r) {
		return ( (x0-Xc)*(x0-Xc)+(y0-Yc)*(y0-Yc) )>=r*r;
	}
	
	public static boolean CheckAngles(double x0,double y0,double x1,double y1,double x2,double y2,double x3,double y3) {
		return ( ( (x0-x1)*(y0-y3)-(x0-x3)*(y0-y1) )*( (x2-x1)*(x2-x3)+(y2-y1)*(y2-y3) ) +
				( (x0-x1)*(x0-x3) + (y0-y1)*(y0-y3) )*( (x2-x1)*(y2-y3) - (x2-x3)*(y2-y1) ) )>=0;
	}
	
	public static boolean CheckAnglesModefied(double x0,double y0,double x1,double y1,double x2,double y2,double x3,double y3) {
		
		if( (((x0-x1)*(x0-x3) + (y0-y1)*(y0-y3))>=0) && (((x2-x1)*(y2-y3) - (x2-x3)*(y2-y1))>=0) ) return true;
		if( (((x0-x1)*(x0-x3) + (y0-y1)*(y0-y3))<0) && (((x2-x1)*(y2-y3) - (x2-x3)*(y2-y1))<0) ) return false;
		
		return ( ( (x0-x1)*(y0-y3)-(x0-x3)*(y0-y1) )*( (x2-x1)*(x2-x3)+(y2-y1)*(y2-y3) ) +
				( (x0-x1)*(x0-x3) + (y0-y1)*(y0-y3) )*( (x2-x1)*(y2-y3) - (x2-x3)*(y2-y1) ) )>=0;
	}
	
}
